﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
public partial class admin_志愿者管理_审核志愿者申请书_Detail : BLL.BasePage_Admin
{
    Volunteer_Apply bll_Volunteer_Apply = new Volunteer_Apply();
    User_Volunteer bll_User_Volunteer = new User_Volunteer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UIInit();
        }
    }

    private void UIInit()
    {
        Bind_Detail();
    }

    private void Bind_Detail()
    {
        int id = QueryString_Int("id");
        Model.Volunteer_Apply m = bll_Volunteer_Apply.GetModel(id);
        lblAge.Text = m.Age.ToString();
        lblDistrct.Text = new Helper_District().Get_FullPath_DistrictId(m.DistrictId, Helper_District.ShowType.斜线连接);
        lblMail.Text = m.Email;
        lblName.Text = m.Name;
        lblPhoneNumber.Text = m.PhoneNumber;
        lblQQ.Text = m.QQ;
        lblSex.Text = m.Sex;
        lblSlogan.Text = m.Slogan;
        lblInsTime.Text = m.InsTime.ToString();
        if (m.IsPass || m.IsReject)
        {
            txtRemark.Text = m.Remark;
            txtRemark.Enabled = false;
            btnPass.Visible = false;
            btnReject.Visible = false;
            lblStatus.Visible = true;
        }
        if (m.IsPass)
        {
            lblStatus.ForeColor = System.Drawing.Color.Green;
            lblStatus.Text = "于 " + m.DisposeTime.ToString() + " 【通过】";
        }
        else if (m.IsReject)
        {
            lblStatus.ForeColor = System.Drawing.Color.Blue;
            lblStatus.Text = "于 " + m.DisposeTime.ToString() + " 【拒绝】";
        }
    }
    private Model.Volunteer_Apply Get_DetailModel()
    {
        int id = QueryString_Int("id");
        return bll_Volunteer_Apply.GetModel(id);
    }
    private bool MyValidate()
    {
        if (string.IsNullOrEmpty(txtRemark.Text))
        {
            Alert("请填写审核人评价！");
            return false;
        }
        return true;
    }
    protected void btnPass_Click(object sender, EventArgs e)
    {
        if (!MyValidate())
        {
            return;
        }
        Model.Volunteer_Apply m = Get_DetailModel();
        m.IsPass = true;
        m.IsReject = false;
        m.Remark = Common.SQLSafe.BeSafe_To_HTML(txtRemark.Text);
        m.DisposeTime = DateTime.Now;
        int id = 0;//用于储存此志愿者在志愿者表中的主键
        if (bll_User_Volunteer.Apply_Pass(m, ref id))
        {
            bll_Volunteer_Apply.Update(m);
            Ini_Role(m, id);//初始化此志愿者的权限列表
            Response.Redirect(Request.Url.PathAndQuery);
        }
        else
        {
            Alert("经查询现有志愿者清单，此邮箱已存在，不能重复添加邮箱相同的志愿者，请检查后协调。");
        }
    }
    /// <summary>
    /// 初始化已通过志愿者的权限
    /// </summary>
    /// <param name="m"></param>
    private void Ini_Role(Model.Volunteer_Apply m, int id)
    {
        Model.Role r = new Model.Role();
        r.Des = m.Name + "的权限设置";
        r.FunctionIdList = ",";
        r.PageIdList = ",";
        Model.User_Volunteer v = bll_User_Volunteer.GetModel(id);
        v.RoleId = new BLL.Role().Add(r);
        bll_User_Volunteer.Update(v);
    }
    protected void btnReject_Click(object sender, EventArgs e)
    {
        if (!MyValidate())
        {
            return;
        }
        Model.Volunteer_Apply m = Get_DetailModel();
        m.IsPass = false;
        m.IsReject = true;
        m.Remark = Common.SQLSafe.BeSafe_To_HTML(txtRemark.Text);
        m.DisposeTime = DateTime.Now;
        bll_Volunteer_Apply.Update(m);
        Response.Redirect(Request.Url.PathAndQuery);
    }
}